/*
* @Author: Josh
* @Date:   2019-01-27 09:44:09
* @Last Modified by:   Administrator
* @Last Modified time: 2019-04-14 15:37:29
*/
'use strict';
require('page/common/nav/index.js');
require('page/common/header/index.js');
require('./index.css');
var navSide       = require('page/common/nav-side/index.js'),
    _mall         = require('util/mall.js'),
    _user         = require('service/user-service.js');

//page 逻辑部分
var page = {
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        //初始化左侧菜单
        navSide.init({
            name : 'user-pass-update'
        });
    },
    bindEvent : function(){
        var _this = this;
        //修改个人信息
        $('#submit').click(function(){
            var userInfo = {
                    password        : $.trim($('#password').val()),
                    passwordNew     : $.trim($('#password-new').val()),
                    passwordConfirm : $.trim($('#password-confirm').val())
                },
                validateResult = _this.validateForm(userInfo);

            if(validateResult.status){
                //更改用户信息
                _user.updatePassword({
                    passwordOld : userInfo.password,
                    passwordNew : userInfo.passwordNew
                }, function(res, msg){
                    _mall.successTips(msg);
                    window.location.href = './user-center.html';
                }, function(errMsg){
                    _mall.errorTips(errMsg);
                });
            }
            else{
                _mall.errorTips(validateResult.msg);
            }
        });
    },
    //验证字段信息
    validateForm : function(formData){
        var result = {
            status : false,
            msg    : '',
        };
        if(!_mall.validate(formData.password, 'require')){
            result.msg = '请输入原密码！';
        }
        else if(!_mall.validate(formData.passwordNew, 'require')){
            result.msg = '请输入新密码！';
        }
        //验证密码长度
        else if(formData.passwordNew.length < 6){
            result.msg = '密码长度不能少于6位！';
        }
        //验证确认密码是否正确
        else if(formData.passwordNew !== formData.passwordConfirm){
            result.msg = '两次输入的密码不一致！';
        }
        else{
            //通过验证，返回正确提示
            result.status = true;
            result.msg    = '验证成功';
        }
        return result;
    }
}; 
    
$(function(){
    page.init(); 
});